import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    goodsList:[],
    carlist:[]
  },
  mutations: {
    ADDSLIST(state,goodsList){
      state.goodsList = goodsList
      // console.log(state.goodsList)
    },
    GOODSPRICE(state,cids){
      if(cids == 0){
        state.goodsList = []
        axios.get('http://localhost:8080/goodsList.json').then((res)=>{
          state.goodsList = res.data.result.data.list
          console.log(state.goodsList)
        })
      }else if(cids == 1){
        state.goodsList.sort(function(a,b){
          return a.actualPrice - b.actualPrice
        })
      }else if(cids == 2){
        state.goodsList.sort(function(a,b){
          return b.actualPrice - a.actualPrice
        })
      }
    },
    CARSLIST(state,item){
      state.carlist.push(item)
      // console.log(state.carlist)
      localStorage.carlist = JSON.stringify(state.carlist)
    }
  },
  actions: {
    ADDSLIST(context,goodsList){
      context.commit("ADDSLIST",goodsList)
    },
    GOODSPRICE(context,cids){
      context.commit("GOODSPRICE",cids)
    },
    CARSLIST(context,item){
      context.commit('CARSLIST',item)
    }
  },
  modules: {
    
  }
})
